/* See the file "LICENSE" for the full license governing this code. */
package com.plugandcare.graphyty.gui;

import java.util.ArrayList;
import java.util.List;

import org.achartengine.ChartFactory;
import org.achartengine.model.XYMultipleSeriesDataset;
import org.achartengine.renderer.XYMultipleSeriesRenderer;

import android.content.ContentResolver;
import android.database.Cursor;
import android.graphics.Color;
import android.util.Log;

import com.plugandcare.data.VitalSignProviderContants;
import com.plugandcare.graphyty.R;

/**
 * @author Timo Tischler
 * @author Steven Mohr
 * 
 */
public class BloodPressureActivity extends AbstractChartActivity {
    private static final ChartType CHART_TYPE = ChartType.BLOOD_PRESSURE;

    private ContentResolver cr = null;

    private Cursor c = null;

    private List<double[]> getSystolicValues() {

        List<double[]> valuesAL = null;

        if (c != null) {
            valuesAL = new ArrayList<double[]>();
            double[] valuesArray = new double[c.getCount()];
            Log.e(LOG_TAG, String.valueOf(c.getCount()));
            c.moveToFirst();
            for (int i = 0; i < c.getCount(); i++) {
                valuesArray[i] = c.getDouble(c.getColumnIndex(VitalSignProviderContants.SYSTOLIC));
                c.moveToNext();
            }
            valuesAL.add(valuesArray);

        }
        return valuesAL;
    }

    private List<double[]> getDiastolicValues() {
        List<double[]> valuesAL = null;
        if (c != null) {
            valuesAL = new ArrayList<double[]>();
            double[] valuesArray = new double[c.getCount()];
            c.moveToFirst();
            for (int i = 0; i < c.getCount(); i++) {
                valuesArray[i] = c.getDouble(c.getColumnIndex(VitalSignProviderContants.DIASTOLIC));
                c.moveToNext();
            }
            valuesAL.add(valuesArray);
        }

        return valuesAL;
    }

    private List<double[]> getPulseValues() {
        List<double[]> valuesAL = null;

        if (c != null) {
            valuesAL = new ArrayList<double[]>();
            double[] valuesArray = new double[c.getCount()];
            c.moveToFirst();
            for (int i = 0; i < c.getCount(); i++) {
                valuesArray[i] = c.getDouble(c.getColumnIndex(VitalSignProviderContants.PULSE));
                c.moveToNext();
            }
            valuesAL.add(valuesArray);
        }
        return valuesAL;
    }

    @Override
    protected void refresh() {
        cr = getContentResolver();
        c =
            cr.query(VitalSignProviderContants.BloodPressureColumns.CONTENT_URI,
                VitalSignProviderContants.BloodPressureColumns.COLUMN_NAMES, null, null, null);
        double[][] hoursAndMinutes = getHoursAndMinutes(CHART_TYPE);

        double[] xArray = null;
        if (c != null) {
            xArray = new double[c.getCount()];
            c.moveToFirst();
            for (int i = 0; i < c.getCount(); i++) {
                xArray[i] = hoursAndMinutes[0][i] + hoursAndMinutes[1][i];
            }

            List<double[]> x = new ArrayList<double[]>();
            x.add(xArray);

            List<double[]> systolicValues = getSystolicValues();

            List<double[]> diastolicValues = getDiastolicValues();

            List<double[]> pulseValues = getPulseValues();

            XYMultipleSeriesRenderer renderer = createRenderer(3);

            XYMultipleSeriesDataset dataset = buildDataset(new String[] { getString(R.string.systolic) }, x, systolicValues);

            setChartSettings(renderer, getString(R.string.blood_pressure), getString(R.string.time), getString(R.string.unit_mmhg),
                hoursAndMinutes[0][0], hoursAndMinutes[0][c.getCount() - 1] + 2, 0, 200,
                Color.LTGRAY, Color.LTGRAY);
            renderer.setXLabels((int) (getMax(hoursAndMinutes[0]) - getMin(hoursAndMinutes[0]) + 2));

            addXYSeries(dataset, new String[] { getString(R.string.diastolic) }, x, diastolicValues, 0);

            addXYSeries(dataset, new String[] { getString(R.string.pulse) }, x, pulseValues, 0);

            setContentView(ChartFactory.getLineChartView(this, dataset, renderer));
        } else {
            createEmptyChart(CHART_TYPE);
        }
    }

}
